/**
 * 文件名:UserController.java
 * 包名:com.wx.demo.wcd.controller
 * 创建时间:2017年3月16日上午10:00:32
 * <p>Copyright ®医界互联 TUS 平台版权所有。</p>
 *
 */

package com.wx.demo.wcd.controller;

import java.util.UUID;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.wx.demo.wcd.domain.po.TbuserPO;
import com.wx.demo.wcd.domain.qo.TbuserQO;
import com.wx.demo.wcd.pub.ReturnCode;
import com.wx.demo.wcd.pub.ReturnData;
import com.wx.demo.wcd.pub.ReturnMsg;
import com.wx.demo.wcd.service.TbuserService;
import com.wx.demo.wcd.utils.MD5Util;
import com.wx.demo.wcd.utils.MailUtil;

/**
 * 类名:UserController <br/>
 * 创建时间: 2017年3月16日 上午10:00:32 <br/>
 * 
 * 〈功能简述〉. <br/>
 * 〈功能详细描述〉.<br/>
 * 
 * @author 王笑 Email:wangxiao@yijiehulian.com.
 * @version
 * @since JDK 1.8
 * @see
 */
@Controller
@RequestMapping("/user")
public class UserController {

	private Logger logger = Logger.getLogger(getClass());

	@Autowired
	private TbuserService userService;

	/**
	 * 
	 * getRegisterPage:注册页面跳转. <br/>
	 *
	 * @author 王笑 Email:wangxiao@yijiehulian.com.;
	 * @return
	 * @since JDK 1.8
	 */
	@RequestMapping("/getRegisterPage")
	public String getRegisterPage() {
		return "user/register.jsp";
	}

	/**
	 * 
	 * getHelpPage:密码找回页面跳转. <br/>
	 *
	 * @author 王笑 Email:wangxiao@yijiehulian.com.;
	 * @return
	 * @since JDK 1.8
	 */
	@RequestMapping("/getHelpPage")
	public String getHelpPage() {
		return "user/pwdHelp.jsp";
	}

	/**
	 * 
	 * getLoginPage:跳转到登录页面. <br/>
	 *
	 * @author 王笑 Email:wangxiao@yijiehulian.com.;
	 * @return
	 * @since JDK 1.8
	 */
	@RequestMapping("/getLoginPage")
	public String getLoginPage() {
		return "login.jsp";
	}

	/**
	 * 
	 * login:用户登录方法. <br/>
	 *
	 * @author 王笑 Email:wangxiao@yijiehulian.com.;
	 * @param user
	 * @return
	 * @since JDK 1.8
	 */
	@ResponseBody
	@RequestMapping(value = "/login", method = RequestMethod.POST)
	public ReturnData<TbuserPO> login(TbuserPO user) {
		ReturnData<TbuserPO> returnData = new ReturnData<TbuserPO>();
		try {
			TbuserQO condition = new TbuserQO();
			// 用户名
			String loginName = user.getLoginName();
			// 密码
			String pwd = user.getLoginName();
			// MD5加密
			String encryptpwd = MD5Util.MD5(pwd);

			condition.setLoginName(loginName);
			condition.setPwd(encryptpwd);

			if (loginName != null && !"".equals(loginName)) {
				java.util.List<TbuserPO> lus = userService.selectCondition(
						condition).getPageList();
				if (lus.size() > 0) {
					returnData.setReturnCode(ReturnCode.SUCCESS);
					returnData.setReturnMsg(ReturnMsg.SUCCESS);
				} else {
					returnData.setReturnCode(ReturnCode.PASSWORD_ERR);
					returnData.setReturnMsg(ReturnMsg.PASSWORD_ERR);
				}
			}
		} catch (Exception e) {
			returnData.setReturnCode(ReturnCode.EXCEPTION);
			returnData.setReturnMsg(ReturnMsg.EXCEPTION);
			logger.info(e);
		}
		return returnData;
	}

	/**
	 * 
	 * register:用户注册方法. <br/>
	 *
	 * @author 王笑 Email:wangxiao@yijiehulian.com.;
	 * @param user
	 * @return
	 * @since JDK 1.8
	 */
	@ResponseBody
	@RequestMapping(value = "/register", method = RequestMethod.POST)
	public ReturnData<TbuserPO> register(@ModelAttribute TbuserPO user) {
		ReturnData<TbuserPO> returnData = new ReturnData<TbuserPO>();
		try {
			// 密码
			String pwd = user.getPwd();
			if (pwd != null && !"".equals(pwd)) {
				// MD5加密
				String encryptpwd = MD5Util.MD5(pwd);
				user.setPwd(encryptpwd);
				int rows = userService.insert(user);
				if (rows > 0) {
					String uid1 = UUID.randomUUID().toString()
							.replaceAll("-", "");
					String uid2 = UUID.randomUUID().toString()
							.replaceAll("-", "");
					String uuid = uid1 + uid2;
					MailUtil.sendMail(user.getEmail(), uuid);
					returnData.setReturnCode(ReturnCode.SUCCESS);
					returnData.setReturnMsg(ReturnMsg.SUCCESS);
				} else {
					returnData.setReturnCode(ReturnCode.ADD_FAILED);
					returnData.setReturnMsg(ReturnMsg.ADD_FAILED);
				}
			} else {
				returnData.setReturnCode(ReturnCode.ADD_FAILED);
				returnData.setReturnMsg(ReturnMsg.ADD_FAILED);
			}
		} catch (Exception e) {
			returnData.setReturnCode(ReturnCode.PARAMETER_ERR);
			returnData.setReturnMsg(ReturnMsg.PARAMETER_ERR);
			logger.info(e);
		}
		return returnData;
	}

	/**
	 * 
	 * activation:用户激活方法. <br/>
	 *
	 * @author 王笑 Email:wangxiao@yijiehulian.com.;
	 * @param code
	 * @return
	 * @since JDK 1.8
	 */
	@RequestMapping("/activation")
	public String activation(String code) {
		if (code != null && !"".equals(code)) {
			return "login.jsp";
		} else {
			return "login.jsp";
		}

	}

}
